package com.technology.education.ui.student.evaluate

import android.view.Gravity
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams
import com.angcyo.dsladapter.DslAdapterStatusItem
import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.updateOrInsertItem
import com.google.android.flexbox.FlexboxLayout
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.SpKeys
import com.technology.education.dslitem.ItemSingleEdit
import com.technology.education.dslitem.ItemSingleImage
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import com.technology.education.widget.CommonButton
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.get

/**
 *@author:create by BZY
 *Date: 2022/3/22 08
 */
class EvaluateFragment : BaseRecyclerTopBarFragment<EvaluateViewModel>() {
    override fun initViews() {
        setTitle("订单评价")
        initGrid(4)
        mViewModel.orderId = arguments?.get("id", "") ?: ""
    }

    override fun lazyLoadData() {
        loading()
    }

    override fun createObserver() {
        mViewModel.tagList.observe(this) {
            parseState(it, onSuccess = { list ->
                setUI(list)
            }, onError = {
                setUI(null)
            })
            loadFinish()
        }

        mViewModel.result.observe(this) {
            if (it) {
                postEventBus(SpKeys.REFRESH_ORDER_DETAIL,true)
                navigation(
                    requireView(), R.id.action_evaluateFragment_to_evaluateResultFragment,
                    bundleOf("id" to mViewModel.orderId)
                )
            }
        }
    }

    private fun setUI(list: List<String>?) {
        renderAdapter {
            setAdapterStatusEnable(false)
            this + ItemSingleText("您对本次订单服务满意吗？").apply {
                style.itemSpan = -1
                style.titleTextSize = 18f
                style.bottomPadding = 6
            }
            dslItem(R.layout.item_evaluate_button) {
                itemSpanCount = -1
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemTag = "button"
                    itemHolder.v<CommonButton>(R.id.btn_good)?.apply {
                        background =
                            getResId(if (mViewModel.commentResult == 1) R.drawable.bg_button else R.drawable.bg_white_round_blue_line)
                        setTextColor(getColor(if (mViewModel.commentResult == 1) R.color.white else R.color.theme_color))
                    }
                    itemHolder.v<CommonButton>(R.id.btn_bad)?.apply {
                        background =
                            getResId(if (mViewModel.commentResult == 0) R.drawable.bg_button else R.drawable.bg_white_round_blue_line)
                        setTextColor(getColor(if (mViewModel.commentResult == 0) R.color.white else R.color.theme_color))
                    }
                    itemHolder.click(R.id.btn_good) {
                        mViewModel.commentResult = 1
                        updateAdapterItem()
                    }
                    itemHolder.click(R.id.btn_bad) {
                        mViewModel.commentResult = 0
                        updateAdapterItem()
                    }
                }
            }
            list?.let {
                dslItem(R.layout.item_single_flexbox) {
                    itemSpanCount = -1
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemTag = "tags"
                        itemBottomInsert = 6 * dpi
                        itemHolder.v<FlexboxLayout>(R.id.fl_content)?.apply {
                            removeAllViews()
                            it.forEach {
                                addView(TextView(requireContext()).apply {
                                    text = it
                                    setTextColor(getColor(if (mViewModel.selectedTags.contains(it)) R.color.white else R.color.text_m))
                                    textSize = 14f
                                    gravity = Gravity.CENTER
                                    setPadding(8 * dpi, 4 * dpi, 8 * dpi, 4 * dpi)
                                    background =
                                        getResId(if (mViewModel.selectedTags.contains(it)) R.drawable.bg_button else R.drawable.round_search_history)
                                    setOnClickListener { _ ->
                                        if (mViewModel.selectedTags.contains(it)) {
                                            mViewModel.selectedTags.remove(it)
                                        } else {
                                            mViewModel.selectedTags.add(it)
                                        }
                                        updateAdapterItem()
                                    }
                                })
                            }
                        }
                    }
                }
            }
            this + ItemSingleEdit(
                hintS = "请描述您对这笔订单的看法",
                tag = "input",
                maxLine = 8,
                minLine = 6
            ).apply {
                style.itemSpan = -1
            }
//            dslItem(R.layout.item_upload_image) {
//                itemTopInsert = 24 * dpi
//                itemSpanCount = 1
//                itemLeftInsert=12*dpi
//                itemDecorationColor = getColor(R.color.white)
//                itemTag = "upload"
//                itemBindOverride = { itemHolder, _, _, _ ->
//                    itemHolder.click(R.id.iv_upload) {
//                        selectPhoto(
//                            3,
//                            selectedList = mViewModel.selectedPhotos as ArrayList<Photo>
//                        ) {
//                            mViewModel.selectedPhotos = it
//                            insertImage(it)
//                        }
//                    }
//                }
//            }
        }
        setBottomButton("立即评价") {
            mViewModel.commentContent = myAdapter.getItemText("input") ?: ""
            mViewModel.commitData()
        }
    }

    private fun insertImage(photos: ArrayList<Photo>) {
        renderAdapter {
            photos.forEach { photo ->
                updateOrInsertItem<ItemSingleImage>(photo.path, dataItems.size - 1) {
                    it.apply {
                        itemTopInsert = 24 * dpi
                        itemDecorationColor = getColor(R.color.white)
                        imgUrl = photo.path
                        itemSpanCount = 1
                    }
                }
            }
        }
    }

}